
const app = getApp()
import Config from "../../common/config.js";
var sessionId = wx.getStorageSync('sessionId')
console.log(sessionId);
Page({
  /**
   * 页面的初始数据
   */
  data: {
    bookList:{},
    avatarUrl:"",
    nickName:"",
    infoState:true,
    bookid:"",
    userId:"",
    status:"",
    token:"",
    isShouAuthorizeModel: false, // 授权弹窗
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(this.data.status);

    // app.onLaunch();
    console.log(options.userId);
    this.setData({
      userId: options.userId
    })
    let that = this;
   
 
    // h5进入时页面跳转处理
    if(options.path){
      wx.switchTab({
        url: options.path,
      })
    }
   
  
  },
  // 第一次进入小程序获取授权
  onGotUserInfo(e) {
    var that = this 
    that.setData({
      infoState: true
    })

    // var sessionId = wx.getStorageSync('sessionId')
    wx.request({
      url: Config.url + '/api/user/updateData',
      method: 'POST',
      header: {
        'content-type': 'application/json',
        'accessToken': that.data.token
      },
      data:{
        icon: e.detail.userInfo.avatarUrl,
        nickName: e.detail.userInfo.nickName
      },
      success(res) {
        console.log(res);
      }
    })
    console.log(e.detail.errMsg)
    console.log(e.detail.userInfo)
    console.log(e.detail.rawData)
  },
  // 作书
  goMakebook: function () {
    wx.switchTab({
      url: '../bookTemplate/bookTemplate'
    })
  },
  // 预览
  goPrev: function(e) {
    console.log(e.currentTarget.dataset.id);
    if (e.currentTarget.dataset.id == 1 || e.currentTarget.dataset.id==2){
      if (e.currentTarget.dataset.id == 2){
        wx.navigateTo({
          url: '../aboutUs/aboutUs',
        })
      }else{
        wx.navigateTo({
          url: '../help/help',
        })
      }
      return
    }else{
      wx.navigateTo({
        url: '../previe/previe?id=' + e.currentTarget.dataset.id
      })
    }
  },
  // 长按删除
  deleteImage:function(e){
    let obj = this;
    console.log("长按触发", e.currentTarget.dataset.id);
    this.setData({
      bookid: e.currentTarget.dataset.id
    })
    if (e.currentTarget.dataset.id == 1 || e.currentTarget.dataset.id==2){
      wx.showToast({
        title: '此书不能删除',
        icon:"none"
      })
      return
    }else{
      wx.showModal({
        title: '提示',
        content: '确定删除这本书吗',
        success: function (res) {
          if (res.confirm) {
            console.log("点击了确定按钮");
            wx.request({
              url: Config.url + '/api/book/delete_book',
              method: 'POST',
              header: {
                'content-type': 'application/json',
                'accessToken': wx.getStorageSync('sessionId')
              },
              data: {
                id: obj.data.bookid
              },
              success(res) {
                wx.request({
                  url: Config.url + '/api/bookshelf/getDataList',
                  method: 'POST',
                  header: {
                    'content-type': 'application/json',
                    'accessToken': wx.getStorageSync('sessionId')
                  },
                  success(res) {
                    console.log(res.data.data.data.books);
                    obj.setData({
                      bookList: res.data.data.data.books,
                    })
                  }
                })
              }
            })

          } else if (res.cancel) {
            console.log("点击了取消按钮")
          }
        }
      })
    }
 
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (op) {
    var that = this;
    // -----------
    wx.login({
      success: function (res) {
        console.log(res.code);
        //发送请求
        wx.request({
          url: 'https://zhangyushu.liangziqishi.shop/api/app/login', //接口地址
          data: {
            code: res.code,
            // userId: "34"
          },
          method: 'POST',
          header: {
            'content-type': 'application/json' //默认值
          },
          success: function (res) {
            console.log(res.data.data.data.accessToken);
            that.setData({ token: res.data.data.data.accessToken});
            wx.request({
              url: Config.url + '/api/bookshelf/getDataList',
              method: 'POST',
              header: {
                'content-type': 'application/json',
                'accessToken': that.data.token
              },
              success(res) {
                that.setData({
                  bookList: res.data.data.data.books,
                  status: res.data.data.data.status
                })
                console.log(that.data.status);
                if (that.data.status == 2) {
                  wx.getSetting({
                    success(res) {
                      console.log(res.authSetting['scope.userInfo'])
                      if (res.authSetting['scope.userInfo']) {
                        that.setData({
                          infoState: false
                        })
                      }else {
                        that.setData({
                          isShouAuthorizeModel: true
                        })
                      }
                    }
                  })
                }
                console.log(that.data.status)
              }
            })
          }
        });
      }
    })
    // ----------
   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  closeAuthorizeModel: function () {
    this.setData({
      isShouAuthorizeModel: false
    })
  }
})